package com.cypher.encryptionanddecryption.fragment.dialog;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.EmptyActivity;
import com.cypher.encryptionanddecryption.activity.MainActivity;
import com.cypher.encryptionanddecryption.adapter.BaseAdapter;
import com.cypher.encryptionanddecryption.bean.OtaBean;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothGattManager;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothManagerSingleton;
import com.cypher.encryptionanddecryption.bluetooth.ExpandBroadcastReceiver;
import com.cypher.encryptionanddecryption.dao.OtaDao;
import com.cypher.encryptionanddecryption.dao.impl.OtaDaoImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author: 古霸天
 * @date: 2021/4/1
 */
public class OtaFragment extends Fragment {
    private ListView listView;
    private MainActivity mainActivity;
    private BluetoothGattManager gattManager;
    private BaseAdapter baseAdapter;
    private OtaDao otaDao;
    private boolean setAdapter = false;
    List<OtaBean> otaBeans;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_maintain_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }


    private void init (View view) {
        // 获取主线程activity
        mainActivity = (MainActivity) BluetoothManagerSingleton.getInstance().getActivity();
        baseAdapter = new BaseAdapter<OtaBean>(view.getContext());
        listView = view.findViewById(R.id.maintain_list);
        gattManager = BluetoothManagerSingleton.getInstance().getBluetoothGattManager();
        otaDao = new OtaDaoImpl();
        otaBeans = new ArrayList<>();
        // 渲染列表数据
        readerListView();
        setListener();
    }

    /**
     * 渲染列表数据
     */
    protected void readerListView() {
        Thread th = new Thread(()->{
            // 保证网络畅通
            if (ExpandBroadcastReceiver.network) {
                otaBeans = otaDao.findAll();
            }
        });
        th.start();
        try {
            th.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!ExpandBroadcastReceiver.network) {
            Toast.makeText(mainActivity, "网络不可用，请检查网络连接", Toast.LENGTH_SHORT).show();
        }
        baseAdapter.set(otaBeans);
        baseAdapter.notifyDataSetChanged();
        // 第一次需要向列表设置适配器
        if (!setAdapter) {
            listView.setAdapter(baseAdapter);
            setAdapter = true;
        }
    }
    @SuppressLint({"WrongConstant", "NewApi"})
    private void setListener() {
        listView.setOnItemClickListener((AdapterView<?> parent, View v, int position, long id) -> {
            if (gattManager == null || !gattManager.isGattConnected()) {
                Toast.makeText(mainActivity, "请先连接蓝牙", Toast.LENGTH_SHORT).show();
                return;
            }
            OtaBean otaBean = otaBeans.get(position);
            Intent intent = new Intent(mainActivity.getApplicationContext(), EmptyActivity.class);
            intent.putExtra("title", otaBean.getName());
            intent.putExtra("data", JSONObject.toJSONString(otaBean));
            intent.putExtra("dataType", "OTA");
            startActivity(intent);
        });
    }

}
